<?php
class Admin_myaccount_Controller extends Template_Controller{
	
	public $template = 'admin/index';
	public $mr;	
	public $mlist;
    public $search;

	function __construct()
	{
		parent::__construct();
        		
		$this->search = array('keyword' => '','sql_where' => '', 'page' => 0);
		
		$this->session->keep_flash('sess_search');
	}
	
	function _get_submit()
	{
		if($this->session->get('error_msg'))
			$this->template->error_msg = $this->session->get('error_msg');
		if($this->session->get('warning_msg'))
			$this->template->warning_msg = $this->session->get('warning_msg');
		if($this->session->get('success_msg'))
			$this->template->success_msg = $this->session->get('success_msg');
		if($this->session->get('info_msg'))
			$this->template->info_msg = $this->session->get('info_msg');
		if($this->session->get('frm')){			
			$this->template->content->mr = $this->session->get('frm');
		}
	}
    
	function index()
	{
		$this->template->content = new View('admin_myacc/frm');
		
        $par = $this->login_model->get_login(2); 
        $model = new Administrator_Model();
        $data = $model->getfromfield($par['id'],'admin_id');        
        $this->mr = $data;
		$this->template->content->mr = $this->mr;
		$this-> _get_submit();
	}
	
	function _get_record()
    {
    	$record = array
	    (
	        'txt_name' => '',
	        'txt_email' => '',
            'txtusername' => '',
            'txtpass' => '',
	    );
	    
	    $errors = $record;

		if($_POST)
    	{
    		$post = new Validation($_POST);
    		
			$post->pre_filter('trim', TRUE);
			$post->add_rules('txt_email','required');
			
			if($post->validate())
 			{
 				$form = arr::overwrite($record, $post->as_array());
 				return $form; 				
			} else {
				$errors = arr::overwrite($errors, $post->errors('myacc_validation'));
				
				$error_msg = '';
				foreach($errors as $id => $name)
					if($name) $error_msg .= '<br>'.$name;
				$this->session->set_flash('error_msg',$error_msg);
				url::redirect('admin_myaccount');
			}
        }		
    }

	function save()
	{
        $par = $this->login_model->get_login(2);
		$record1 = $this->_get_record();	
		$user_id = $par['id'];
        
		$record2 = array(
			'admin_email' =>$record1['txt_email'],
            'admin_name' =>$record1['txtusername'],
		);
        
        if($record1['txtpass'] != "" )
        {
            $record3 = array('admin_pass' => md5($record1['txtpass']),);
            $record = array_merge($record2,$record3);
        } else { 
            $record =$record2; 
        }
        
        $model = new Administrator_Model();
		$result = $model->update($user_id, $record);
		   
		if($result)
            $this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_save'));	
		else
			$this->session->set_flash('success_msg',Kohana::lang('errormsg_lang.msg_data_error'));	
		url::redirect('admin_myaccount');
		die();
	}
}
?>